import 'package:flutter/material.dart';
import '../model/health_record.dart';
import '../service/database_service.dart';
import 'base_record_dialog.dart';

class AddHeightRecordDialog extends BaseRecordDialog {
  const AddHeightRecordDialog({Key? key, required String babyId})
    : super(key: key, babyId: babyId);

  @override
  _AddHeightRecordDialogState createState() => _AddHeightRecordDialogState();
}

class _AddHeightRecordDialogState
    extends BaseRecordDialogState<AddHeightRecordDialog> {
  final _heightController = TextEditingController();

  @override
  void dispose() {
    _heightController.dispose();
    super.dispose();
  }

  @override
  String getDialogTitle() => '记录身高';

  @override
  Widget buildCustomContent(BuildContext context) {
    return TextField(
      controller: _heightController,
      decoration: InputDecoration(
        labelText: '身高',
        suffixText: 'cm',
        border: OutlineInputBorder(),
      ),
      keyboardType: TextInputType.numberWithOptions(decimal: true),
    );
  }

  @override
  Future<void> saveRecord(BuildContext context) async {
    final height = double.tryParse(_heightController.text);
    if (height == null || height <= 0 || height > 200) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('请输入有效的身高值')));
      return;
    }

    try {
      await DatabaseService().addHealthRecord(
        HealthRecord(
          babyId: widget.babyId,
          type: RecordType.height,
          timestamp: selectedDateTime,
          data: {'height': height, 'notes': notes},
        ),
      );
      Navigator.pop(context);
    } catch (e) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('保存失败: $e')));
    }
  }
}
